package com.xinchuang.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinchuang.comment.R;
import com.xinchuang.domain.entity.TestMaterialsBaseInfo;
import com.xinchuang.mapper.TestMaterialsBaseInfoMapper;
import com.xinchuang.service.TestMaterialsBaseInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: thh
 * @Date: 2024/4/14 20:27
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TestMaterialsBaseInfoServiceImpl extends ServiceImpl<TestMaterialsBaseInfoMapper, TestMaterialsBaseInfo> implements TestMaterialsBaseInfoService {

    private final TestMaterialsBaseInfoMapper testMaterialsBaseInfoMapper;

    @Override
    public String addMaterialsBaseInfo(List<TestMaterialsBaseInfo> list) {
        boolean b = this.saveBatch(list);
        if (b) {
            return JSON.toJSONString(R.success("保存成功"));
        }
        return JSON.toJSONString(R.fail("保存失败"));
    }

    @Override
    public String editMaterialsBaseInfo(List<TestMaterialsBaseInfo> list) {
        String prop = list.get(0).getProp();
        testMaterialsBaseInfoMapper.delete(new QueryWrapper<TestMaterialsBaseInfo>().lambda().eq(TestMaterialsBaseInfo::getProp, prop));
        boolean b = this.saveBatch(list);
        if (b) {
            return JSON.toJSONString(R.success("编辑成功"));
        }
        return JSON.toJSONString(R.fail("编辑失败"));
    }

    @Override
    public String deleteMaterialsBaseInfo(JSONObject jsonObject) {
        String prop = jsonObject.getString("prop");
        testMaterialsBaseInfoMapper.delete(new QueryWrapper<TestMaterialsBaseInfo>().lambda().eq(TestMaterialsBaseInfo::getProp, prop));
        return JSON.toJSONString(R.success("删除成功"));
    }

    @Override
    public String listMaterialsBaseInfo(JSONObject jsonObject) {
        //0:通用;1:内部;2:byd;3:奇瑞;4:东风;5:吉利
        Integer type = jsonObject.getInteger("type");
        //名称 （模糊搜索）
        String name = jsonObject.getString("name");
        //属性
        String prop = jsonObject.getString("prop");
        //子类
        String subType = jsonObject.getString("subType");
        //jsonobject解析获取页码
        //String pageNum = jsonObject.getString("pageNum");
        //jsonobject解析获取每页数量
        //String pageSize = jsonObject.getString("pageSize");

        //Page<TestMaterialsBaseInfo> testMaterialsBaseInfoPage = new Page<>(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        LambdaQueryWrapper<TestMaterialsBaseInfo> testMaterialsBaseInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        testMaterialsBaseInfoLambdaQueryWrapper.eq(Objects.nonNull(type), TestMaterialsBaseInfo::getType, type);
        testMaterialsBaseInfoLambdaQueryWrapper.eq(StringUtils.isNotEmpty(prop), TestMaterialsBaseInfo::getProp, prop);
        testMaterialsBaseInfoLambdaQueryWrapper.eq(StringUtils.isNotEmpty(subType), TestMaterialsBaseInfo::getSubType, subType);
        testMaterialsBaseInfoLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), TestMaterialsBaseInfo::getName, name);
        testMaterialsBaseInfoLambdaQueryWrapper.orderByDesc(TestMaterialsBaseInfo::getCreateTime);
        List<TestMaterialsBaseInfo> testMaterialsBaseInfoList = testMaterialsBaseInfoMapper.selectList(testMaterialsBaseInfoLambdaQueryWrapper);
        //Page<TestMaterialsBaseInfo> page = testMaterialsBaseInfoMapper.selectPage(testMaterialsBaseInfoPage, testMaterialsBaseInfoLambdaQueryWrapper);
        Map<String, Object> result = new HashMap<>(2);
        Map<String, List<TestMaterialsBaseInfo>> map = testMaterialsBaseInfoList.stream().collect(Collectors.groupingBy(data -> data.getProp() + ""));
       // result.put("totalNum", map.size());
        result.put("dataList", map);

        return JSON.toJSONString(R.data(result));
    }
}
